/**
 * author:Administrator
 * date:2015/7/27
 * desc:TODO
 */

'use strict';
var CapturerBase = require('./CapturerBase.js');
var CapturerTour = require('./CapturerTour.js');
var __isDebug = true;
/**
 * 领域词语捕获
 * @param isDebug {boolean} 是否debug模式
 * @constructor
 */
function ChainCapturer(isDebug) {
  __isDebug = isDebug || false;
  this.__layer = [];
}
module.exports = ChainCapturer;


ChainCapturer.prototype.addCapturer = function (aCapturer) {
  if (!aCapturer instanceof BaseCapturer) {
    throw new Error('addFilter args must instanceof BaseFilter');
  }
  this.__layer.push(aCapturer);
};

ChainCapturer.prototype.doCapture = function (srcText) {
  for (var aCapturer of this.__layer) {
    aCapturer.capture(srcText);
  }
};

/**
 * 捕获chain没有任何顺序相关性,与ChainCapturer不同
 * @param isDebug isDebug mode
 * @returns {ChainCapturer}
 */
ChainCapturer.defaultChainCapturer = function (isDebug) {
  var aChainCapturer = new ChainCapturer(isDebug || false);

  // add capture
  aChainCapturer.addCapturer(new CapturerBase('wordBaby'),true);
  aChainCapturer.addCapturer(new CapturerBase('wordFamily'),true);
  aChainCapturer.addCapturer(new CapturerBase('wordFashion'),true);
  aChainCapturer.addCapturer(new CapturerBase('wordLove'),true);
  aChainCapturer.addCapturer(new CapturerBase('wordMam'),true);
  aChainCapturer.addCapturer(new CapturerBase('wordShow'),true);
  aChainCapturer.addCapturer(new CapturerBase('wordTour'),true);

  return aChainCapturer;
};

ChainCapturer.prototype.showAndClean = function (capturerName) {
  if (!__isDebug) {
    console.log('FilterChain not in debug mode');
    return;
  }

  if (capturerName !== undefined && typeof capturerName === "number") {
    //console.log('number');
    var aCapturer = this.__layer[capturerName];
    if (aCapturer) aCapturer.showAndClean();
    else console.log('max is ' + this.__layer.length - 1);
  } else if (capturerName !== undefined) {
    //console.log('filterName');
    for (var aCapturer of this.__layer) {
      if (aCapturer.constructor.name === capturerName) {
        aCapturer.showAndClean();
      }
    }
  } else {
    //console.log('all');
    for (var aCapturer of this.__layer) {
      aCapturer.showAndClean();
    }
  }
};